define(function (require, exports, module) {

    require("apps/grid");
    require("apps/easyui");

    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "directory_template"
            },
            configs: {
                gridTitle: "角色列表",
                gridUrl: ctx + "/app/api/directory/findDirectories",
                gridColumns: [
                    {field: "id", checkbox: true},
                    {field: "directoryType.dirGroupValue", title: "字典组", width: 100},
                    {field: "dirCode", hidden: true, title: "字典编码", width: 200},
                    {field: "dirValue", title: "字典值", width: 300},
                    {field: "dirOrder", title: "序号值", width: 100},
                    {field: "dirDescription", hidden: true, title: "字典项描述"}
                ]
            },
            events: {
                renderColBarBefore: function (btn, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "edit":
                            whenEdit(context, btn.href, row);
                            break;
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "字典项搜索", "directory_search");
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        case "del":
//                            whenDel(context, btn.href);
                            context.del(btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();

        function whenEdit(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "directory_edit_save",
                title: "编辑字典项",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            context.detail("字典项详情", row);
        }

        function whenAdd(context, href) {
            globalWindow.create({
                href: href,
                method: "directory_add_save",
                title: "新增字典项",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenDel(context, href) {
            var rows = context.grid.datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert('信息', '请先选择一行', 'info');
                context.grid.datagrid('clearSelections');
                return;
            }
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].directoryType.dirGroup == "HOUR_DAY_CONFIG") {
                    $.messager.alert('信息', '天数换算配置不可删除', 'info');
                    return;
                }
            }
            if (rows.length > 0) {
                $.messager.confirm('确定', '确定要删除吗?', function (result) {
                    if (result) {
                        var ids = [];
                        for (var i = 0; i < rows.length; i++) {
                            ids.push(rows[i].id);
                        }
                        $.post(
                            href,
                            $.utils.buildJson({ids: ids}),
                            function (json) {
                                if ($.isErrorRequest(json)) {
                                    $.messager.alert('信息', json.msg, 'info');
                                } else {
                                    $.messager.alert('信息', '删除成功', 'info');
                                    context.grid.datagrid("reload");
                                }
                            }
                        );
                    }
                });
            }
        }

    };

    exports.add = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid);

        window.directory_add_save = function(win) {
            var postForm = page.getJqDom("form-directory-info");
            if (!postForm.form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var postData = postForm.serializeJson();
            $.post(postForm.attr("action") + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (data) {
                    win.wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.update = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid);

        window.directory_edit_save = function(win) {
            var postForm = page.getJqDom("form-directory-info");
            if (!postForm.form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var postData = postForm.serializeJson();
            $.post(postForm.attr("action") + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (data) {
                    win.wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.search = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid, true);

        window.directory_search = function(win) {
            win.wait("start", "搜索中,请稍候.");
            var form = page.getJqDom("form-directory-info");
            var postData = form.serializeJson();
            win.callback(postData);
            win.close();
        }
    }
});